#!/usr/bin/env Python
#-*-utf-8-*-
#author:zby
#TODO：21点游戏

target =21

#玩家名字
user1 = input('first player:')
user2 = input('second player:')

#存储玩家的名字和获胜次数
user_dict = {
    user1:
    {'win':0},
    user2:
    {'win':0}
}

is_again = True
while is_again:
    # 两个玩家猜测的数
    user1_guess = input('first player num:')
    while not user1_guess.isnumeric():
        print('输入的数据有误，请重新输入数字')
        user1_guess = input('first player num:')
    user2_guess = input('second player num:')
    while not user2_guess.isnumeric():
        print('输入的数据有误，请重新输入数字')
        user2_guess = input('first player num:')
    # 产生两个随机的数
    import random
    num1 = random.randint(1, 10)
    num2 = random.randint(1, 10)
    print(num1, num2)

    # 相加后判断与21的差的绝对值决定输赢
    user1_num = int(user1_guess) + num1 + num2
    user2_num = int(user2_guess) + num1 + num2
    if abs(user1_num-21) > abs(user2_num):
        print(f'{user1_num}',f'{user2_num}')
        print(f'{user2} win!')
        user_dict[user2]['win'] += 1
    elif abs(user1_num-21) < abs(user2_num):
        print(f'{user1_num}',f'{user2_num}')
        print(f'{user1} win!')
        user_dict[user2]['win'] += 1
    else:
        print(f'{user1_num}',f'{user2_num}')
        print('平局')
    # 打印显示每个玩家的获胜局数
    for k,v in user_dict.items():
        print(f'{k}的获胜次数：{v.get("win")}')
    # 判断是否需要进行下一局
    is_again = input('是否要退出(q/n)').strip() != 'q'